/**
 * 
 */
package cx3k.GUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;

import cx3k.scheduler.Scheduler;
import cx3k.scheduler.objects.Course;

/**
 * @author Chris
 *
 */
public class ProfilePane extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField jtfName;
	private JComboBox jcbMajor, jcbMinor;
	private CompletedClasses FinishedClasses;
	private JLabel jlCCValue, jlSCValue;
	private Scheduler scheduler;
	private String[] majors, minors;
	private ProfileListener PL;
	private ClassInfo classInfo;
	
	/**
	 * 
	 * @param s Scheduler
	 * @param classInfo ClassInfo
	 */
	public ProfilePane(Scheduler s, ClassInfo classInfo) {
		GridBagLayout gbl = new GridBagLayout();
		this.setLayout(gbl);
		GridBagConstraints gbc = new GridBagConstraints();
		this.setBackground(MainWindow.MAINCOLOR);
		this.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
		this.scheduler = s;
		this.classInfo = classInfo;
		majors = s.getMajorStrings();
		minors = s.getMinorStrings();
		this.PL = new ProfileListener();
		
		JLabel jlName = new JLabel("Name:");
		gbc.gridx = 0;
		gbc.gridy = 0; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 30; 
		gbc.weighty = 10; 
		gbc.fill = GridBagConstraints.NONE; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(jlName, gbc);
		this.add(jlName); 
		
		this.jtfName = new JTextField();
		this.jtfName.addActionListener(PL);
		gbc.gridx = 1;
		gbc.gridy = 0; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 70; 
		gbc.weighty = 10; 
		gbc.fill = GridBagConstraints.HORIZONTAL; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(jtfName, gbc);
		this.add(jtfName); 
		
		JLabel jlMajor = new JLabel("Major / Double Major:");
		gbc.gridx = 0;
		gbc.gridy = 1; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 30; 
		gbc.weighty = 10; 
		gbc.fill = GridBagConstraints.NONE; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(jlMajor, gbc);
		this.add(jlMajor); 
	
		this.jcbMajor = new JComboBox();
		addToComboBox(majors, jcbMajor);
		jcbMajor.addItemListener(PL);
		
		jcbMajor.setCursor(new Cursor(Cursor.HAND_CURSOR));
		gbc.gridx = 1;
		gbc.gridy = 1; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 70; 
		gbc.weighty = 10; 
		gbc.fill = GridBagConstraints.HORIZONTAL; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(jcbMajor, gbc);
		this.add(jcbMajor);

		JLabel jlMinors = new JLabel("Additional Minor:");
		gbc.gridx = 0;
		gbc.gridy = 2; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 70; 
		gbc.weighty = 10; 
		gbc.fill = GridBagConstraints.NONE; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(jlMinors, gbc);
		this.add(jlMinors);
		
	
		this.jcbMinor = new JComboBox();
		jcbMinor.setCursor(new Cursor(Cursor.HAND_CURSOR));
		addToComboBox(this.minors, jcbMinor);
		jcbMinor.addItemListener(PL);
		
		
		
		gbc.gridx = 1;
		gbc.gridy = 2; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 70; 
		gbc.weighty = 10; 
		gbc.fill = GridBagConstraints.HORIZONTAL; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(jcbMinor, gbc);
		this.add(jcbMinor); 
		
		
		JLabel c = new JLabel("Completed Classes:");
		gbc.gridx = 0;
		gbc.gridy = 3; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 10; 
		gbc.weighty = 25; 
		gbc.fill = GridBagConstraints.NONE; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(c, gbc);
		this.add(c);
		
		this.FinishedClasses = new CompletedClasses(classInfo, s);
		JScrollPane scrollClasses = new JScrollPane(FinishedClasses);
		gbc.gridx = 0;
		gbc.gridy = 4; 
		gbc.gridwidth = 2; 
		gbc.gridheight = 2; 
		gbc.weightx = 100; 
		gbc.weighty = 30; 
		gbc.fill = GridBagConstraints.BOTH; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(scrollClasses, gbc);
		this.add(scrollClasses);
		
		JLabel jlCCredits = new JLabel("Completed Credits:  ");
		this.jlCCValue = new JLabel("0");
		
		Box hBox = Box.createHorizontalBox();
		hBox.add(jlCCredits);
		hBox.add(jlCCValue);
		
		gbc.gridx = 0;
		gbc.gridy = 6; 
		gbc.gridwidth = 2; 
		gbc.gridheight = 1; 
		gbc.weightx = 10; 
		gbc.weighty = 10; 
		gbc.fill = GridBagConstraints.NONE; 
		gbc.anchor = GridBagConstraints.LINE_START; 
		gbl.setConstraints(hBox, gbc);
		this.add(hBox);
		
		
	}
	
	/*
	private void comboAdd(JComboBox jcb, ItemListener IL) {
		for (int i = 0; i < jcb.getItemCount(); i++) {
			Jcjcb.getComponent(i)i;
		}
	}
	*/
	
	/**
	 * Update the GUI with info in the profile.
	 */
	public void updateProfile() {
		this.jtfName.setText(scheduler.getName());
		this.jcbMajor.setSelectedIndex(scheduler.getMajor()+1);
		this.jcbMinor.setSelectedIndex(scheduler.getMinor()+1);
		this.FinishedClasses.update(scheduler.getCompletedClasses());
		
		for (Course c: scheduler.getCompletedClasses()) {
			System.out.println(c.getName());
		}
		
		this.updateUI();
	}
	
	public boolean checkErrors() {
		boolean b = true;
		ErrorFrame ef = new ErrorFrame();
		if (this.jtfName.getText().equalsIgnoreCase("Your name here.")) {
			ef.addErrorText("No name was entered.");
			b = false;
		}
		
		if (this.jcbMajor.getSelectedIndex() == 0) {
			ef.addErrorText("You must select a major.");
			b = false;
		}
		
		if (!b) ef.showFrame();
		
		return b;
	}
	
	/**
	 * Adds all of the strings to the comboBox
	 * @param s String[]: Strings to add to comboBox
	 * @param jcb JComboBox: 
	 */
	private void addToComboBox(String s[], JComboBox jcb) {
		jcb.addItem(" ");
		for (int i = 0; i < s.length; i++) {
			if (!s[i].equalsIgnoreCase("GFR")) jcb.addItem(s[i]);
		}
	}

	
	

	/**
	 * Class that responds to changes in profile information.
	 * @author Chris
	 *
	 */
	private class ProfileListener implements ActionListener, ItemListener {

		@Override
		public void actionPerformed(ActionEvent ae) {
			// TODO Auto-generated method stub
			if (ae.getSource() == jtfName) {
				scheduler.updateName(jtfName.getText());
			}
		}

		@Override
		public void itemStateChanged(ItemEvent ie) {
			// TODO Auto-generated method stub
			if (ie.getSource() == jcbMajor) {
				scheduler.updateMajor(jcbMajor.getSelectedIndex());
				System.out.println("Major: " + jcbMajor.getSelectedIndex());
			}
			else if (ie.getSource() == jcbMinor) {
				scheduler.updateMajor(jcbMinor.getSelectedIndex());
				System.out.println("Minorr: " + jcbMinor.getSelectedIndex());
			}
		}
		
	}

	
	public CompletedClasses getCompletedClasses() {
		return this.FinishedClasses;
	}
	public String getName() { return jtfName.getText(); }
	public void setName(String name) { jtfName.setText(name); }
	
	public String getMajor() { return jcbMajor.getSelectedItem().toString(); }
	
	public int getCCValue() { return Integer.parseInt(jlCCValue.getText()); }
	public void setCCValue(int credits) { jlCCValue.setText(new Integer(credits).toString()); }
	
	public int getSCValue() { return Integer.parseInt(jlSCValue.getText()); }
	public void setSCValue(int credits) { jlSCValue.setText(new Integer(credits).toString()); }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}